package www.admin.controller;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import www.admin.model.BorrowMoney;
import www.admin.model.Channel;
import www.admin.model.LendingPlatform;
import www.admin.mybatis.Criteria;
import www.admin.service.IBorrowLogService;
import www.admin.service.IBorrowMoneyService;
import www.admin.service.IChannelService;
import www.admin.service.ILendingPlatformService;
import www.admin.utils.BaseController;
import www.admin.utils.DateUtil;
import www.admin.utils.ExportExcel;
import www.admin.utils.JsonResult;
import www.admin.utils.Layui;
import www.admin.utils.StringUtil;

/**
 * 借款
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value="/borrow")
public class BorrowMoneyController extends BaseController{

	@Autowired
	private IBorrowMoneyService service;
	
	@Autowired
	private IChannelService channelService;
	
	@Autowired
	private ILendingPlatformService lendingPlatformService;
	
	@Autowired
	private IBorrowLogService borrowLogService;
	
	/**
	 * 借款页面
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/showlist")
	public String showList(ModelMap map){
		Criteria<Channel> param = new Criteria<Channel>();
		param.addParam("status", 0);
		map.put("channelList", channelService.queryPageList(param));
		Criteria<LendingPlatform> params = new Criteria<LendingPlatform>();
		params.addParam("status", 0);
		map.put("lendingList", lendingPlatformService.queryPageList(params));
		return "borrowMoney/borrowMoneyList";
	}
	
	/**
	 * 借款用户页面
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/selectlist")
	public String selectlist(ModelMap map){
		return "borrowMoney/selectUserInfo";
	}
	
	/**\
	 * 借款列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/querypage")
	@ResponseBody
	public Layui queryPageList(HttpServletRequest request){
		Criteria<BorrowMoney> param = new Criteria<BorrowMoney>();
		param.setStartIndex(Integer.parseInt(request.getParameter("page")));
		param.setPageSize(Integer.parseInt(request.getParameter("limit")));
		if (!StringUtil.isNullOrBlank(request.getParameter("key"))) {
			param.addParam("key", request.getParameter("key"));
		}
		if (!StringUtil.isNullOrBlank(request.getParameter("startDate"))) {
			param.addParam("startDate", request.getParameter("startDate"));
		}
		if (!StringUtil.isNullOrBlank(request.getParameter("endDate"))) {
			param.addParam("endDate", request.getParameter("endDate"));
		}
		if (!StringUtil.isNullOrBlank(request.getParameter("startTime"))) {
			param.addParam("startTime", request.getParameter("startTime"));
		}
		if (!StringUtil.isNullOrBlank(request.getParameter("endTime"))) {
			param.addParam("endTime", request.getParameter("endTime"));
		}
		if (!StringUtil.isNullOrBlank(request.getParameter("channelId"))) {
			param.addParam("channelId", request.getParameter("channelId"));
		}
		if (!StringUtil.isNullOrBlank(request.getParameter("lendingId"))) {
			param.addParam("lendingId", request.getParameter("lendingId"));
		}
		if (!StringUtil.isNullOrBlank(request.getParameter("status"))) {
			param.addParam("status", request.getParameter("status"));
		}
		if (!getRoleName(request).equals(RETURN_NUMBER_NAME)) {
			param.addParam("seed_user", getUserId(request));
		}
		return service.queryPage(param);
	}
	
	/**
	 * 跳转添加页面
	 * @param id
	 * @return
	 */
	@RequestMapping("/detail")
	public String detail(String id,ModelMap map) {
		Criteria<Channel> param = new Criteria<Channel>();
		param.addParam("status", 0);
		map.put("channelList", channelService.queryPageList(param));
		Criteria<LendingPlatform> params = new Criteria<LendingPlatform>();
		params.addParam("status", 0);
		map.put("lendingList", lendingPlatformService.queryPageList(params));
		map.put("borrowMoney", service.queryEntity(id));
		return "borrowMoney/borrowMoneyAdd";
	}
	
	/**
	 * 跳转备注页面
	 * @param id
	 * @return
	 */
	@RequestMapping("/addremark")
	public String addRemark(String id,ModelMap map) {
		map.put("borrowMoney", service.queryEntity(id));
		return "borrowMoney/addRemarkAdd";
	}
	
	/**
	 * 添加借款
	 * @param id
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@ResponseBody
	@RequestMapping(value = "/insert", method = { RequestMethod.POST })
	public JsonResult insert(HttpServletRequest request){
		JsonResult jr = JsonResult.newInstance();
		BorrowMoney money = new BorrowMoney();
		money.setId(getSeqNum());
		money.setUserId(request.getParameter("userId"));
		money.setBusinessMan(request.getParameter("businessMan"));
		money.setChannelId(request.getParameter("channelId"));
		money.setLendingId(request.getParameter("lendingId"));
		money.setStartTime(DateUtil.parse(request.getParameter("startTime")));
		money.setEndTime(DateUtil.parse(request.getParameter("endTime")));
		money.setTotalMoney(new BigDecimal(request.getParameter("totalMoney")));
		money.setTotalAccrual(new BigDecimal(request.getParameter("totalAccrual")));
		money.setSeedUser(getUserId(request));
		String ret = service.insert(money);
		if (StringUtil.isNullOrBlank(ret)) {
			ret = insertOpera(request.getParameter("record")+money.getId());
		}
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret);
	}
	
	/**
	 * 修改借款
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/update", method = { RequestMethod.POST })
	public JsonResult update(BorrowMoney money){
		JsonResult jr = JsonResult.newInstance(); 
		Criteria<BorrowMoney> param = new Criteria<BorrowMoney>();
		param.setRecord(money);
		param.addParam("id", money.getId());
		String ret = service.update(param);
		if (StringUtil.isNullOrBlank(ret)) {
			ret = insertOpera(money.getRecord()+money.getId());
		}
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret); 
	}
	
	/** 
	 * 导出excel 
	 * @return 
	 */  
	@ResponseBody
	@RequestMapping(value = "/export")
	public String excelPage(HttpServletResponse response,String id){
		Criteria<BorrowMoney> param = new Criteria<BorrowMoney>();
		if (!StringUtil.isNullOrBlank(id)) {
			param.addParam("idList", getResourceId(id));
		}
		List<Map<String,Object>> list = service.queryPageList(param);
	    String header="客户姓名:userName,客户电话号码:mobile,渠道来源:channelName,业务员:businessMan,借款金额:totalMoney,还款金额:repayMoney,放款平台:lendingName,借款时间:startTime,还款日期:endTime";
	    ExportExcel.excel(response, list, "借款列表", header);
	    return "success";
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete")
	public JsonResult delete(String id,String record,String userName) {
		JsonResult jr = JsonResult.newInstance();
        String ret = service.deleteById(id);
		if (StringUtil.isNullOrBlank(ret)) {
			ret = insertOpera(record+userName);
		}
		borrowLogService.deleteById(id);
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret);
	}

}
